<?php 
// this file and function checked admin session value stored or not
require_once '../model/testimonials.php';
require_once '../model/moduleuser.php';	
require_once '../includes/classes/pagination_class.php';	


// create object
$testimonialObj = new Model_Testimonial(); 
$moduleObj      = new Model_ModuleUser();


// Help Text 
if($_GET['q']=='testimonials')
{
	$moduelArray=$moduleObj->getModuleHelpText(9);
	$smarty->assign('moduelArray', $moduelArray);
}

$page = $_GET['page']; 
## Active, Inactive selected records from selection box 
/*******************************/
if(isset($_POST['go']) != '' && isset($_POST['action']) != '') {

		$ids = implode(",", $_POST['checkall']);
		## check which action is selected
		if(trim($ids) != '' ) {
			$hidden_page = $_POST['hidden_pageno'];	

			if($_POST['action'] == 'delete') {
				## Delete selected records
				$testimonialObj->deletemultiTestimonials($ids);
				$_SESSION['msg']="<div class='success_msg'><span>Testimonial(s) deleted successfully</span></div>";
			}			
			
			if($_POST['action'] == 'active') {
				## Delete selected records
				$testimonialObj->updateTestimonialStatus($ids, '1');
				$_SESSION['msg']="<div class='success_msg'><span>Testimonial(s) status  changed successfully</span></div>";
			}
						
			if($_POST['action'] == 'inactive') {
				## Delete selected records
				$testimonialObj->updateTestimonialStatus($ids, '0');
				$_SESSION['msg']="<div class='success_msg'><span>Testimonial(s) status changed successfully</span></div>";
			}
			
				header('location:'.SITE_URL.'/admin/home.php?q=testimonials&page='.$hidden_page);
				exit;
		}
		
} // if close	

//deleting testionial by clicking on the delete icon
if(isset($_GET['action']) && isset($_GET['id']) != '') {
	
	if($_GET['action'] == 'delete' ){
		$id = base64_decode($_GET['id']);
		## Delete User
		$testimonialObj->deleteTestimonialById($id);
		$_SESSION['msg']="<div class='success_msg'><span>Testimonial deleted successfully!</span></div>";
		header('location:'.SITE_URL.'/admin/home.php?q=testimonials');
		exit;
	}
}


// Admin can chnage Active / Inactive by using images 
	if(isset($_GET['action']) == 'status')
    {
		$id = base64_decode($_GET["id"]);
		// if the status is 1 alredy 
    	if(base64_decode($_GET["status"]))   {
           			
					$arr["status"]='0';
					$testimonialObj->edit_Testimonial($arr, $id);
					$_SESSION['msg']="<div class='success_msg'><span>Testimonial inactivated successfully</span></div>";
					header('location:'.SITE_URL.'/admin/home.php?q=testimonials');
					exit;
		}else{
					$arr["status"]='1';
					$testimonialObj->edit_Testimonial($arr, $id);
					$_SESSION['msg']="<div class='success_msg'><span>Testimonial activated successfully</span></div>";
					header('location:'.SITE_URL.'/admin/home.php?q=testimonials');
					exit;
				}
    }

##Fetch all testimonial  pages from database
## Get search parameters in variables - 
if($_POST['search'] != '' && $_POST['search'] != 'Search By Username') {
	$searchindex = addslashes($_POST['search']);
} else if($_GET['search'] != '') {
	$searchindex = addslashes($_GET['search']);
} else {
	$searchindex = '';
}

$searchindex=trim($searchindex);

	// Assign search variable to show in search textbox
$smarty->assign('search', stripslashes (stripslashes ($searchindex)));

if($_GET['col1'] != '') 
{
	$col1 = base64_decode($_GET['col1']);
} 
else if($_GET['col2'] != '') 
{
	$col2 = base64_decode($_GET['col2']);
} 
else 
{
	$col1 = '0';	
}

##  --------- Pagination part first start --------------##
	if( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] != "" ) {
		$pageNum = $_GET[ 'page' ];
	} else {
		$pageNum = 1;
	} 
	$rowsPerPage = ROW_PER_PAGE;
	if($_GET['col1'] != '') 
	{
		$pageName = "home.php?q=testimonials&col1=".$_GET['col1'].""; ;
	} 
	else if($_GET['col2'] != '') 
	{
		$pageName = "home.php?q=testimonials&col2=".$_GET['col2'].""; ;
	} 	 
	else 
	{
		$pageName = "home.php?q=testimonials"; 
	}

	
	## Count all the records
	$testimonials	=   $testimonialObj->list_Testimonial($searchindex,$col1,$col2, $rowsPerPage, $offset, false);	
	$total_rows = count($testimonials);
	$offset	= ($pageNum - 1) * $rowsPerPage;
	$smarty->assign('page', $pageNum);
	
	//echo "<pre>"; print_r($userArray)."</pre>";
##  --------- Pagination part first end --------------##


##  --------- Pagination part second start --------------##
	$newid = $pageNum * $rowsPerPage - $rowsPerPage + 1;
	$smarty->assign('newid', $newid);

	$testimonials	=   $testimonialObj->list_Testimonial($searchindex,$col1,$col2, $rowsPerPage, $offset, true);
	//echo "<pre>"; print_r($userArray);exit;
	$smarty->assign('testimonials', $testimonials);
	
	if($searchindex != '') {
		$other_id = "search=".$searchindex;
	} else {
		$other_id = '';
	}
	
	if($total_rows > ROW_PER_PAGE) {
		$pg = new pagination();					// CREATING OBJECT OF THE PAGINATION CLASS
		$pgnation = $pg->pagination_admin( $total_rows , $rowsPerPage , $pageNum , $pageName , $other_id, $class);	
		$smarty-> assign("pagination",$pgnation);
	}
##  --------- Pagination part second end --------------##	
	
	
	 
	## Set active class variable for left menu  (main menu and sub menu)
$smarty->assign('activeclass', 'testimonial');
$smarty->assign('mainmenu', '3');

	
	if($_SESSION['msg']){
		$smarty->assign("msg", $_SESSION['msg']);
		unset($_SESSION['msg']);
	}
	

$smarty->display(TEMPLATEDIR_ADMIN . 'testimonials/manage_testimonials.tpl');

?>